# -- plugin -------------------------------------------------------------------

# tmux plugin manager
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# esay motion like vim
set -g @plugin 'schasse/tmux-jump'
set -g @jump-key 'F'

# urlView
set -g @plugin 'tmux-plugins/tmux-urlview'

# yank
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-open'
# run-shell ~/.tmux/plugins/tmux-yank/yank.tmux
set -g @shell_mode 'vi'

# highlight prefix
#set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
#set -g @prefix_highlight_prefix_prompt 'Wait'
#set -g @tmux_power_prefix_highlight_pos 'LR'

# theme
#set -g @plugin "arcticicestudio/nord-tmux"
set -g @plugin 'wfxr/tmux-power'
# set -g @tmux_power_theme '#89f398'

# resurrect
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# restore vim/neovim session
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
set -g @continuum-restore 'on'
set -g @resurrect-capture-pane-contents 'on'

# tmux fzf
set -g @plugin 'laktak/extrakto'
set -g @extrakto_key t
set -g @extrakto_copy_key tab
set -g @extrakto_insert_key enter


# vim
set-option -sa terminal-overrides 'xterm:colors=256'

# -- general -------------------------------------------------------------------

setw -g mode-keys vi
set -g default-terminal "screen-256color" # colors!
setw -g xterm-keys on
set -g mouse on
set -s escape-time 10                     # faster command sequences
set -sg repeat-time 600                   # increase repeat timeout
set -s focus-events on

set -g prefix C-e
unbind C-b
bind C-e send-prefix

#set -g prefix2 C-e                        # GNU-Screen compatible prefix
#bind C-e send-prefix -2

set -q -g status-utf8 on                  # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on

set -g history-limit 5000                 # boost history

# reload configuration
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf sourced'

set -g status-position top

# -- display -------------------------------------------------------------------

set -g base-index 1           # start windows numbering at 1
setw -g pane-base-index 1     # make pane numbering consistent with windows

setw -g automatic-rename on   # rename window to reflect current program
set -g renumber-windows on    # renumber windows when a window is closed

set -g set-titles on          # set terminal title

set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000      # slightly longer status messages display time

set -g status-interval 1     # redraw status line every 10 seconds

# clear both screen and history
bind C-l send-keys C-l \; run 'sleep 0.1' \; clear-history

# all panes will get the input
bind-key s set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"

# activity
set -g monitor-activity on
set -g visual-activity off

# -- navigation ----------------------------------------------------------------

# create session
bind C-c new-session

# find session
unbind C-f
unbind f

#unbind %
#unbind \"
# split current window horizontally
#bind - split-window -v -c "#{pane_current_path}"
# split current window vertically
#bind _ split-window -h -c "#{pane_current_path}"
bind % split-window -v -c "#{pane_current_path}"
bind \" split-window -h -c "#{pane_current_path}"

# pane navigation
bind h select-pane -L  # move left
bind j select-pane -D  # move down
bind k select-pane -U  # move up
bind l select-pane -R  # move right
bind > swap-pane -D       # swap current pane with the next one
bind < swap-pane -U       # swap current pane with the previous one

# maximize current pane
bind + run 'cut -c3- ~/.tmux.function | sh -s _maximize_pane "#{session_name}" #D'

# pane resizing
bind H resize-pane -L 2
bind J resize-pane -D 2
bind K resize-pane -U 2
bind L resize-pane -R 2

# window navigation
#unbind n
#unbind p
#bind -r C-h previous-window # select previous window
#bind -r C-l next-window     # select next window
bind Tab last-window        # move to last active window

bind n next-window
bind p previous-window

# toggle mouse
bind m run "cut -c3- ~/.tmux.function | sh -s _toggle_mouse"

bind x kill-pane
bind X kill-window
bind c new-window

bind [ swap-window -t -1\; select-window -t -1
bind ] swap-window -t +1\; select-window -t +1

# -- urlview -------------------------------------------------------------------

bind U run "cut -c3- ~/.tmux.function | sh -s _urlview #{pane_id}"


# -- list choice (tmux < 2.4) --------------------------------------------------

# vi-choice is gone in tmux >= 2.4
run -b 'tmux bind -t vi-choice h tree-collapse 2> /dev/null || true'
run -b 'tmux bind -t vi-choice l tree-expand 2> /dev/null || true'
run -b 'tmux bind -t vi-choice K start-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice J end-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice H tree-collapse-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice L tree-expand-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice Escape cancel 2> /dev/null || true'


# -- edit mode (tmux < 2.4) ----------------------------------------------------

# vi-edit is gone in tmux >= 2.4
run -b 'tmux bind -ct vi-edit H start-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit L end-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit q cancel 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit Escape cancel 2> /dev/null || true'


# -- copy mode -----------------------------------------------------------------

bind Enter copy-mode # enter copy mode

run -b 'tmux bind -t vi-copy v begin-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true'
run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi H send -X start-of-line 2> /dev/null || true'
run -b 'tmux bind -t vi-copy L end-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi L send -X end-of-line 2> /dev/null || true'

# copy to macOS clipboard
if -b 'command -v pbcopy > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | pbcopy"'
if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
# copy to X11 clipboard
if -b 'command -v xsel > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xsel -i -b"'
if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1"'
# copy to Windows clipboard
if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'


# -- buffers -------------------------------------------------------------------

#bind b list-buffers  # list paste buffers
#bind p paste-buffer  # paste from the top paste buffer
#bind P choose-buffer # choose which buffer to paste from


# -- user defined overrides ----------------------------------------------------

if '[ -f ~/.tmux.conf.local ]' 'source ~/.tmux.conf.local'


# -- version check 8< ------------------------------------------------------------------------

#run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
#run -b '[ -z "#{window_active}" ] && [ -z "#{version}" ] && tmux set display-time 3000 \; display "This configuration will soon require tmux >= 2.4" \; set -u display-time || true'


# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
